package com.pilipala.controller;

import com.pilipala.dto.BlogDTO;
import com.pilipala.dto.BlogSearchDTO;
import com.pilipala.dto.PageinationDTO;
import com.pilipala.dto.UpdateBlogDTO;
import com.pilipala.result.PageResult;
import com.pilipala.result.Result;
import com.pilipala.service.BlogService;
import com.pilipala.vo.BlogVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/blog")
@Slf4j
@Tag(name = "博客相关接口")
public class BlogController {

    @Autowired
    private BlogService blogService;

    /**
     * 添加博客
     * @param blogDTO 博客内容
     * @return 无
     */
    @PostMapping("/create")
    @Operation(summary = "新增blog")
    public Result<?> create(@RequestBody BlogDTO blogDTO){
        log.info("发布blog----");
        blogService.create(blogDTO);
        return Result.success();
    }

    /**
     * 根据条件获取Blog
     * @param blogSearchDTO blog的作者Id，blog的标题，分页的页码和分页大小
     * @return 分页后的数据
     */
    @GetMapping("/get")
    @Operation(summary = "根据条件获取Blog")
    public Result<PageResult> listByCondition(BlogSearchDTO blogSearchDTO){
        log.info("获取blog数据:{}",blogSearchDTO);
        PageResult pageResult = blogService.listByCondition(blogSearchDTO);
        return Result.success(pageResult);
    }

    /**
     * 无条件获取所有Blog
     * @param blogSearchDTO 页码、页大小
     * @return 分页后的结果
     */
    @GetMapping("/list")
    @Operation(summary = "无条件获取所有Blog")
    public  Result<PageResult> list(BlogSearchDTO blogSearchDTO){
        log.info("无条件获取所有的Blog{}",blogSearchDTO);
        PageResult pageResult = blogService.list(blogSearchDTO);
        log.info("参数值:{}",pageResult.getRecords());
        return Result.success(pageResult);
    }

    /**
     * 根据博客Id获取博客
     * @param id 博客Id
     * @return 返沪博客数据
     */
    @GetMapping("/getById/{id}")
    @Operation(summary = "根据博客Id获取博客")
    public Result<BlogVO> getByBlogId(@PathVariable Long id) {
        log.info("根据博客Id获取博客");
        BlogVO blogVO = blogService.getById(id);
        return Result.success(blogVO);
    }

    /**
     * 删除指定blog
     * @param id Blog的Id
     * @return 无
     */
    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除指定blog")
    public Result<?> deleteBlog(@PathVariable Long id){
        log.info("删除指定blog，blog的Id为{}",id);
        blogService.delete(id);
        return Result.success();
    }

    /**
     * 根据blog的Id修改blog
     * @param updateBlogDTO 包括title、content、description、id--blog的
     * @return 无
     */
    @PostMapping("/update")
    @Operation(summary = "根据blog的Id修改blog")
    public Result<?> updateBlog(@RequestBody UpdateBlogDTO updateBlogDTO){
        log.info("根据blog的Id修改blog：{}",updateBlogDTO);
        blogService.update(updateBlogDTO);
        return Result.success();
    }

    @PutMapping("/Collection/{blogId}")
    @Operation(summary = "收藏blog")
    public Result<?> Collection(@PathVariable Long blogId){
        log.info("收藏blog，blog的id为:{}",blogId);
        blogService.collection(blogId);
        return Result.success();
    }

    /**
     * 获取收藏的blog
     * @param pageinationDTO 页码、页大小
     * @return 分页后的数据
     */
    @GetMapping("/getCollection")
    @Operation(summary = "获取收藏的blog")
    public Result<PageResult> getCollection(PageinationDTO pageinationDTO){
        log.info("获取收藏的blog：{}",pageinationDTO);
        PageResult pageResult = blogService.getCollection(pageinationDTO);
        return Result.success(pageResult);
    }

    /**
     * 点赞博客
     * @param blogId 博客Id
     * @return 无
     */
    @PutMapping("/likes/{blogId}")
    @Operation(summary = "点赞博客")
    public Result<?> likes(@PathVariable Long blogId){
        log.info("点赞博客,博客Id:{}",blogId);
        blogService.likes(blogId);
        return Result.success();
    }
}







